package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_stat_keywords;

import cn.com.api.service.ApiU_stat_keywordsService;
import cn.com.api.service.CheckService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "stat_keywords/")
public class ApiU_stat_keywordsController {

	@Autowired
	private ApiU_stat_keywordsService apiU_stat_keywordsService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiU_stat_keywordsController.class);

	/**
	 * 更新喊红包访问数量
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "saveBonusVisitCount")
	public void saveBonusVisitCount(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String keyword, String type, String source) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("keyword", keyword);
			params.put("type", type);
			params.put("source", source);

			if (checkService.checkToken(user_id, token, map)) {
				int ret = this.apiU_stat_keywordsService.SaveBonusVisitCount(params);

				if (ret > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "添加成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
					map.put(Utils.MESSAGE, "添加失败！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

	/**
	 * 新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertU_stat_keywords")
	public void insertU_stat_keywords(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String stat_id = request.getParameter("stat_id");
		String source = request.getParameter("source");
		String ip = request.getParameter("ip");
		String type = request.getParameter("type");
		String pv_count = request.getParameter("pv_count");
		String ip_count = request.getParameter("ip_count");
		String visit_count = request.getParameter("visit_count");
		String keyword = request.getParameter("keyword");
		String create_time = request.getParameter("create_time");
		String user_id = request.getParameter("user_id");
		String page_url = request.getParameter("page_url");
		String entry_url = request.getParameter("entry_url");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
		String update_time = request.getParameter("update_time");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = "";

		try {
			if (stat_id != null && !"".equals(stat_id.trim())) {
				ApiU_stat_keywords u_stat_keywords = new ApiU_stat_keywords();

				// u_stat_keywords.setStat_id(stat_id);
				// u_stat_keywords.setSource(source);
				// u_stat_keywords.setIp(ip);
				// u_stat_keywords.setType(type);
				// u_stat_keywords.setPv_count(pv_count);
				// u_stat_keywords.setIp_count(ip_count);
				// u_stat_keywords.setVisit_count(visit_count);
				// u_stat_keywords.setKeyword(keyword);
				// u_stat_keywords.setCreate_time(create_time);
				// u_stat_keywords.setUser_id(user_id);
				// u_stat_keywords.setPage_url(page_url);
				// u_stat_keywords.setEntry_url(entry_url);
				// u_stat_keywords.setExt_1(ext_1);
				// u_stat_keywords.setExt_2(ext_2);
				// u_stat_keywords.setExt_3(ext_3);
				// u_stat_keywords.setExt_4(ext_4);
				// u_stat_keywords.setExt_5(ext_5);
				// u_stat_keywords.setUpdate_time(update_time);

				apiU_stat_keywordsService.insertU_stat_keywords(u_stat_keywords);
				// return
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_stat_keywordsById")
	public void findU_stat_keywordsById(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiU_stat_keywords u_stat_keywords = apiU_stat_keywordsService.findU_stat_keywordsById(Integer.valueOf(id));

			// return
			map.put("datas", u_stat_keywords);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	/**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_stat_keywordss")
	public void findU_stat_keywordss(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<ApiU_stat_keywords> u_stat_keywordss = apiU_stat_keywordsService.findU_stat_keywordss();

			// return
			map.put("datas", u_stat_keywordss);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateU_stat_keywords")
	public void updateU_stat_keywords(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String stat_id = request.getParameter("stat_id");
		String source = request.getParameter("source");
		String ip = request.getParameter("ip");
		String type = request.getParameter("type");
		String pv_count = request.getParameter("pv_count");
		String ip_count = request.getParameter("ip_count");
		String visit_count = request.getParameter("visit_count");
		String keyword = request.getParameter("keyword");
		String create_time = request.getParameter("create_time");
		String user_id = request.getParameter("user_id");
		String page_url = request.getParameter("page_url");
		String entry_url = request.getParameter("entry_url");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
		String update_time = request.getParameter("update_time");

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (stat_id != null && !"".equals(stat_id.trim())) {
				ApiU_stat_keywords u_stat_keywords = new ApiU_stat_keywords();
				// 获取原数据
				u_stat_keywords = apiU_stat_keywordsService.findU_stat_keywordsById(Integer.valueOf(id));
				// 修改
				// u_stat_keywords.setIs_open(Integer.valueOf(is_open));

				//
				int ret_i = apiU_stat_keywordsService.updateU_stat_keywords(u_stat_keywords);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteU_stat_keywords")
	public void deleteU_stat_keywords(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {

				Integer ret_I = apiU_stat_keywordsService.deleteU_stat_keywords(Integer.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
